package baidu.reader.common
{
    import baidu.reader.common.model.*;
    import baidu.ui.managers.*;
    import flash.display.*;
    import flash.events.*;
    import flash.ui.*;
    import flash.utils.*;

    public class KeyboardManager extends EventDispatcher
    {
        private var stage:Stage;
        private var lastKeyDownTime:int = 0;

        public function KeyboardManager(param1:IEventDispatcher = null)
        {
            super(param1);
            this.initModel();
            return;
        }

        private function initModel() : void
        {
            this.stage = (InstanceManager.createSingletonInstance(ScreenModel) as ScreenModel).stage;
            return;
        }

        public function initListeners() : void
        {
            this.stage.addEventListener(KeyboardEvent.KEY_DOWN, this.onKBDown);
            this.stage.addEventListener(KeyboardEvent.KEY_UP, this.onKBUp);
            return;
        }

        private function onKBDown(event:KeyboardEvent) : void
        {
            this.handleKB(event);
            return;
        }

        private function onKBUp(event:KeyboardEvent) : void
        {
            this.handleKBUp(event);
            return;
        }

        private function handleKBUp(event:KeyboardEvent) : void
        {
            switch(event.keyCode)
            {
                case Keyboard.SPACE:
                {
                    this.nextPage();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        private function checkInterval() : Boolean
        {
            if (getTimer() - this.lastKeyDownTime < 300)
            {
                return false;
            }
            this.lastKeyDownTime = getTimer();
            return true;
        }

        public function handleKB(event:KeyboardEvent) : void
        {
            switch(event.keyCode)
            {
                case Keyboard.RIGHT:
                {
                    this.nextPage();
                    break;
                }
                case Keyboard.PAGE_DOWN:
                {
                    this.nextPage();
                    break;
                }
                case Keyboard.LEFT:
                {
                    this.prevPage();
                    break;
                }
                case Keyboard.PAGE_UP:
                {
                    this.prevPage();
                    break;
                }
                case Keyboard.DOWN:
                {
                    this.moveDown();
                    break;
                }
                case Keyboard.UP:
                {
                    this.moveUp();
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
            return;
        }

        private function moveUp() : void
        {
            dispatchEvent(new ReaderEvent(ReaderEvent.GO_PREV_LINE));
            return;
        }

        private function moveDown() : void
        {
            dispatchEvent(new ReaderEvent(ReaderEvent.GO_NEXT_LINE));
            return;
        }

        private function prevPage() : void
        {
            if (this.checkInterval())
            {
                dispatchEvent(new ReaderEvent(ReaderEvent.GO_PREV_PAGE));
            }
            return;
        }

        private function nextPage() : void
        {
            if (this.checkInterval())
            {
                dispatchEvent(new ReaderEvent(ReaderEvent.GO_NEXT_PAGE));
            }
            return;
        }

    }
}
